package com.whfc.ms.api.base.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.dto.SysWorkAreaDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.SysWorkAreaAddParam;
import com.whfc.fuum.param.SysWorkAreaEditParam;
import com.whfc.ms.api.base.service.WorkAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-09-01 15:21
 */
@RestController
@RequestMapping("/ms/api/work/area")
public class WorkAreaController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WorkAreaService workAreaService;

    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("获取工区列表：loginer:{},deptId:{},pageNum:{},pageSize:{},keyword:{}",
                user.getUsername(), deptId, pageNum, pageSize, keyword);
        PageData<SysWorkAreaDTO> pageData = workAreaService.list(deptId, pageNum, pageSize, keyword);
        return ResultUtil.success(pageData);
    }

    @GetMapping(value = "/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("获取工区列表：loginer:{},deptId:{},keyword:{}", user.getUsername(), deptId, keyword);
        List<SysWorkAreaDTO> list = workAreaService.list(deptId, keyword);
        return ResultUtil.success(new ListData<>(list));
    }

    @RequiresPermissions("prj/area/add")
    @PostMapping(value = "/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody SysWorkAreaAddParam param) {
        logger.info("新增工区：loginer:{},param:{}", user.getUsername(), param);
        workAreaService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("prj/area/edit")
    @PostMapping(value = "/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody SysWorkAreaEditParam param) {
        logger.info("修改工区：loginer:{},param:{}", user.getUsername(), param);
        workAreaService.edit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("prj/area/del")
    @PostMapping(value = "/del/{workAreaId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("workAreaId") Integer workAreaId) {
        logger.info("删除工区：loginer:{},workAreaId:{}", user.getUsername(), workAreaId);
        workAreaService.del(workAreaId);
        return ResultUtil.success();
    }


}
